package com.situ.web.service.impl;

import java.util.List;

import com.situ.web.dao.Impl.CourseDaoImpl;
import com.situ.web.entity.Course;
import com.situ.web.service.IcourseService;
import com.situ.web.vo.PageInfo;

public class CourseServiceImpl implements IcourseService{
	private CourseDaoImpl courseDao = new CourseDaoImpl();
	
	

	@Override
	public List<Course> selectAll() {
		List<Course> list = courseDao.selectAll();
		for(Course course : list) {
			course.setName("2020" + course.getName());
		}
		return list;
	}



	@Override
	public boolean delete(int id) {
		int count = courseDao.delete(id);
//		if (count == 1) {
//			return true;
//		} else {
//			return false;
//		}
		return count != 0 ? true:false;
	}


	@Override
	public boolean insert(Course course) {
		int count = courseDao.insert(course);
		return count == 1 ? true:false;
	}


	@Override
	public PageInfo selectPage(int pageNo, int pageSize) {
		// 在这个service层代码里面就是来封装PageInfo这个对象
		PageInfo pageInfo =  new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// List<Course> list; 当前页面的集合
		int offset = (pageNo - 1) * pageSize;
		List<Course> list = courseDao.selectPage(offset, pageSize);
		pageInfo.setList(list);
		
		int totalCount = courseDao.selectTotalCount();
		int totalPage = (int) Math.ceil((double)totalCount / pageSize);
		pageInfo.setTotalPage(totalPage);
		
		return pageInfo;
	}



	public boolean update(Course course) {
		int count = courseDao.update(course);
		return count == 1 ? true:false;
	}



	public Course selectId(int id) {
		Course course = courseDao.selectId(id);
		return course;
	}
}
